// void, Obj This

Building this;
str names, class_unit, class_captain;
int i, max_unit, r, p;

this = This.AsBuilding();

EnvWriteString(this, "researching", "yes");

.Progress();

EnvWriteString(this, "researching", "no");   

p = .player;

if(.IsValid){
	Unit captain;
        
	names = "/1/Outpost/"+.name;
	EnvWriteInt(names, 1);        

	max_unit = 30;
	//pr(.race);
	r = EnvReadInt(.player, "RACE");        
						
	if (r==Carthage){
		class_captain = "CarthaginianCaptain";
		class_unit = "CLibyanFootmanGuard";
		max_unit = 50;
	} else if (r==Iberia){
		class_captain = "IberianCaptain"; 
		class_unit = "IDefenderGuard";
	} else if (r==Britain){
		class_captain = "BritonCaptain";
		class_unit = "BBronzeSpearmanGuard";
	} else if (r==Germany){
		class_captain = "GermanCaptain";
		class_unit = "BBronzeSpearmanGuard";
	} else if (r==Egypt){
		class_captain = "EgyptianCaptain";
		class_unit = "CLibyanFootmanGuard";
		max_unit = 50;
	} else if (r==Gaul){
		class_captain = "GaulCaptain";
		class_unit = "GSpearmanGuard";
	} else if(r == ImperialRome || r == RepublicanRome){
		class_captain = "RomanCaptain";
		class_unit = "RPrincipleGuard"; 
	} else {
		class_captain = "GaulCaptain";
		class_unit = "GSpearmanGuard";
	}
	//EnvWriteString(this, "SentryBlockOut", "yes");
	//Sleep(1000);
	Sleep(rand(100)+100);
	captain = Place(class_captain, .pos, 14);
	if(captain.IsValid){
		.settlement.ForceAddUnit(captain);
		CreateFeedback("DefensiveCry", captain.AsUnit, -1);
	}
	Sleep(20);
	if(captain.IsValid){
		ObjList olSentry, ol;
		Unit u1;
		int r1, r2;
		str nameu;

		for(i=0; i<max_unit; i+=1){
			Sleep(rand(100)+100);
			u1 = Place(class_unit, .pos, 14);
			if(u1.IsValid){
				//u1.SetLevel(6);
				//u1.SetFeeding(false);
				//u1.SetNoselectFlag(true);
				.settlement.ForceAddUnit(u1);
				olSentry.Add(u1);
				//u1.SetCommand("attach", captain.AsHero);
			}
		}
		Sleep(1000);
		olSentry.ClearDead();
		olSentry.SetCommand("attach", captain.AsHero);
		ol.AddList( olSentry );
		ol.Add( captain.AsUnit );
		//nameo = "/1/Outpost/"+.name;
		//pr(ol.count);
		for(i=0; i<ol.count; i+=1){
			Sleep(rand(50)+10);
			u1 = ol[i].AsUnit;
			if(u1.IsValid){
				if(u1.name == ""){
					r1 = rand(99);
					r2 = rand(99);       
					u1.SetName("U"+u1.player+u1.race+u1.class+r1+r2);
				}
				nameu = "/1/SentryName/"+u1.name;
				EnvWriteString(nameu, names);
			}
		}
		EnvWriteString(this, "SentryOutpostBuying", "yes");
		
	}
}
